﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;



using System.IO; // for OutputTextBlock
using System.Xml; // for XDocument
using System.Xml.Linq; // for XDocument


namespace Bingootest1
{
    
    public partial class Page1 : PhoneApplicationPage
    {

        private string[] lists;
        public Page1()
        {

            InitializeComponent();
            // Set the data context of the listbox control to the sample data
            lists = new string[] { "strategy", "technology", "design" };
        
        }


        // Handle selection changed on ListBox
        private void WordsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         
        }

        // Load data for the ViewModel Items
        private void Page1_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            string srcItem = "";
            base.OnNavigatedTo(e);
            bool itemExists = NavigationContext.QueryString.TryGetValue("selectedItem", out srcItem);
            if (itemExists)
            {
               
                int b = Int32.Parse(srcItem);
                if (b < lists.Length)
                {
                    PageTitle.Text = lists[b];
                }


                WebClient wc = new WebClient();
                wc.OpenReadCompleted += wc_OpenReadCompleted;
                Uri url = new Uri("http://shovel.fn-sandbox.net/bingoo/" + PageTitle.Text + ".xml");
                wc.OpenReadAsync(url);
                    
            }

        }


        private List<String> TotalWordsList = new List<string>();

        private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                String Text = e.Error.Message;
                return;
            }
            using (Stream s = e.Result)
            {
                XDocument doc = XDocument.Load(s);

                IEnumerable<XElement> items = from el in doc.Descendants("word") select el;

                TotalWordsList.Clear();
                ((App)Application.Current).words.Clear();

                foreach (XElement el in items)
                    TotalWordsList.Add(el.Value);                    
            }

            DisplayTotalWordsList();
        }

        private void DisplayTotalWordsList()
        {
            SelectionCounter = 0;
            DataContext = new CheckViewModel(TotalWordsList);

            if (!((CheckViewModel)DataContext).IsDataLoaded)
            {
                ((CheckViewModel)DataContext).LoadData();
            }

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }


        public int SelectionCounter = 0;

        private void ddlDiseaseNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            

        }
        private void UpdateCheckBoxesAndListBox()
        {

   

        }

        private void UpdateText()
        {
            if (SelectionCounter < 4)
            {
                textBlock1.Text = "Please select " + (4-SelectionCounter).ToString() + " more words";
                button1.IsEnabled = false;
            }
            else
            {
                textBlock1.Text = "Press Play";
                button1.IsEnabled = true;

            }

            
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            SelectionCounter++;
            ((App)Application.Current).words.Add( ((TextBlock)((CheckBox)sender).Content).Text );
            UpdateText();

        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            SelectionCounter--;
            ((App)Application.Current).words.Remove(((TextBlock)((CheckBox)sender).Content).Text);
            UpdateText();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            storyboard1.Completed += new EventHandler(MyStoryboard_Completed);
            storyboard1.Begin();

            
        }
        
        /*
             * Add this function to your code file
             */
        void MyStoryboard_Completed(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Game.xaml", UriKind.RelativeOrAbsolute));
        }

  
    }
}